package com.situ.mall.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.situ.mall.common.util.Result;
import com.situ.mall.pojo.entity.product.Category;
import com.situ.mall.pojo.entity.product.Product;
import com.situ.mall.product.pojo.vo.Category13VO;
import com.situ.mall.product.pojo.vo.Category23VO;
import com.situ.mall.product.pojo.vo.CategoryVO;
import com.situ.mall.product.service.ICategoryService;
import com.situ.mall.product.service.IProductService;
import com.situ.mall.product.service.impl.ProductServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品分类 前端控制器
 * </p>
 *
 * @author Sun Huijuan
 * @since 2024-10-09
 */
@RestController
@RequestMapping("/product/category")
public class CategoryController {

	@Autowired
	private ICategoryService categoryService;
	@Autowired
	private IProductService productService;
	
	//【后台管理】——查询树形结构
	@GetMapping("/selectCategoryTree")
	public Result<List<CategoryVO>> selectCategoryTree() {
		List<CategoryVO> list = categoryService.selectCategoryTree();
		return Result.ok(list);
	}
	
	@RequestMapping("/selectByParentId/{id}")
	public Result<List<CategoryVO>> selectByParentId(@PathVariable Long id) {
		List<CategoryVO> list=categoryService.selectByParentId(id);
		return Result.ok(list);
	}
	
	@GetMapping("/selectAllCategory3")
	public Result<List<Category>> selectAllCategory3() {
		List<Category> list=categoryService.selectAllCategory3();
		return Result.ok(list);
	}
	
	//查二三级分类
	@GetMapping("/selectAllCategory23")
	public Result<List<Category23VO>> selectAllCategory23() {
		List<Category23VO> list=categoryService.selectAllCategory23();
		return Result.ok(list);
	}
	
	@GetMapping("/selectAllCategory13")
	public Result<List<Category13VO>> selectAllCategory13() {
		List<Category13VO> list=categoryService.selectAllCategory13();
		return Result.ok(list);
	}
	
	//【后台管理】——修改分类信息
	@PutMapping("/update")
	public Result updateCategory(@RequestBody Category category) {
		categoryService.update(category);
		return Result.ok("修改成功！");
	}
	
	//【后台管理】——删除分类
	@DeleteMapping("/deleteById/{id}")
	public Result deleteById(@PathVariable Long id) {
		//查询该分类下有没有产品
		List<Product> list=productService.selectProductList(id);
		if(list.size()>0){
			return Result.error("请先移动该分类下的商品");
		}
		categoryService.deleteById(id);
		return Result.ok("删除成功！");
	}
	
	//【后台管理】——添加分类
	@PostMapping("/add")
	public Result add(@RequestBody Category category) {
		categoryService.add(category);
		return Result.ok("添加成功！");
	}
	
	//查询单个分类
	@GetMapping("/selectById/{id}")
	public Result<Category> selectById(@PathVariable Long id){
		Category category=categoryService.getById(id);
		return Result.ok(category);
	}
	
	//【远程调用——查询单个分类】
	@GetMapping("/selectCategory/{id}")
	public Result<Category> selectCategory(@PathVariable Long id){
		Category category=categoryService.getById(id);
		return Result.ok(category);
	}
	
	@GetMapping("/selectCategory1Count")
	public Result selectCategory1Count(){
		QueryWrapper<Category> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("parent_id",0L);
		Long count=categoryService.count(queryWrapper);
		return Result.ok(count);
	}
}

